﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Business.Models;
using System.Data;

namespace Business.DAO
{
    public class KhoiLopDAO
    {
        private ISSContext db = new ISSContext();
        public List<KhoiLop> GetAll()
        {
            return db.KhoiLop.ToList();
        }

        public List<KhoiLop> GetByTruongHoc(int idTruongHoc) 
        {
            try
            {
                return db.KhoiLop.Where(m => m.IdTruongHoc == idTruongHoc).ToList();
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        public KhoiLop Get(int id)
        {
            KhoiLop KhoiLop = db.KhoiLop.Find(id);
            return KhoiLop;
        }

        public int Create(KhoiLop KhoiLop)
        {
            try
            {
                db.KhoiLop.Add(KhoiLop);
                return db.SaveChanges();
            }
            catch (Exception ex)
            {
                return 0;
            }
        }

        public int Edit(KhoiLop KhoiLop)
        {
            try
            {
                db.Entry(KhoiLop).State = EntityState.Modified;
                return db.SaveChanges();
            }
            catch (Exception ex)
            {
                return 0;
            }
        }

        public int Delete(int id)
        {
            try
            {
                KhoiLop KhoiLop = db.KhoiLop.Find(id);
                db.KhoiLop.Remove(KhoiLop);
                return db.SaveChanges();
            }
            catch (Exception ex)
            {
                return 0;
            }
        }

        public List<KhoiLop> GetByQuanTri(QuanTri QuanTri)
        {
            var query = from khoilop in db.KhoiLop where khoilop.QuanTri == QuanTri select khoilop;
            List<KhoiLop> lstKhoiLop = query.ToList<KhoiLop>();
            return lstKhoiLop;
        }

        public List<KhoiLop> GetByQuanTri_TruongHoc(TruongHoc TruongHoc, QuanTri QuanTri)
        {
            var query = from khoilop in db.KhoiLop where khoilop.QuanTri == QuanTri && khoilop.TruongHoc == TruongHoc select khoilop;
            List<KhoiLop> lstKhoiLop = query.ToList<KhoiLop>();
            return lstKhoiLop;
        }
    }
}